inBrowser = false;
inW8StoreApp = false;
inAndroidApp = false;
inWPApp = false;
insidePaginaJuego = false;


var CurrentGameInfo = new Object();

//Indica si se esta en la págian de jeugo o  no



function GoToHome(){
    $.mobile.changePage("#PaginaInicial")	
}
function GoToGame(){
    $.mobile.changePage("#PaginaJuego")	
}
function GoToInfoPage(){
    $.mobile.changePage("#PaginaInfo")	
}

var eventToHearForTap = "tap"
var eventToHearForMousePress = "mousedown"
var eventToHearForMouseRelease = "mouseup"
var eventToHearForMouseMove = "mousemove"


gameLoop = function () {
	if(insidePaginaJuego){		
		DrawGame(CurrentGameInfo);
	}else {
	
	}
	setTimeout(
		gameLoop, 
		15
	);
};

$( document ).ready(function() {
	 detectPageOrientation();
	 $(window).resize(detectPageOrientation);
	 
    console.log(navigator.userAgent)
	try{
		var testA = (document.URL.indexOf( 'http://' ) == -1 )
		var testB = (document.URL.indexOf( 'https://' ) == -1)
		var testC = (document.URL.indexOf( 'file://' ) == -1 )		
		var app =  testB && testA && testC;
		if(app){			
		    if (navigator.userAgent.indexOf("MSAppHost") != -1) {
		        inW8StoreApp = true;		        
		    }
			if(navigator.userAgent.indexOf("Android") != -1){
				inAndroidApp = true;
			}
			if(navigator.userAgent.indexOf("Windows Phone") != -1){
				inWPApp = true;
			}
		}else{
		    inBrowser = true;
		    
		}
	}catch(err){		
		inBrowser = true;		
	}
	
		
	if(inWPApp){
		$("#GamePageHeader").remove();
		var drawCanvas = $("#GameDrawCanvas").get()[0]
		console.log("Default cw "+drawCanvas.width);
		console.log("Default ch "+drawCanvas.height);
		drawCanvas.width = Window_Width;
		drawCanvas.height = Window_Height;
		//drawCanvas.style.width = Window_Width;
		//drawCanvas.style.height = Window_Height;
	}
	if(inBrowser){
		var drawCanvas = $("#GameDrawCanvas").get()[0]
		drawCanvas.width = 1280;
		drawCanvas.height = 768;
	}
	if (window.navigator.msPointerEnabled) {        
		console.log("msPointerEnabled")  
		eventToHearForMousePress = "MSPointerDown"
		eventToHearForMouseRelease = "MSPointerUp"
		eventToHearForMouseMove = "MSPointerMove"
		eventToHearForTap = "MSPointerUp"
		MSEventsInterfacePresent = true;
	}else{
		console.log("msPointer NOT Enabled")
		eventToHearForTap = "tap"
	}
	
   //Cargar lista de juegos
   SYS_timeInfo = time.getInfo();
   LoadGames()
   
   var BotonInformacion =  $("#BotonInformacion");
   BotonInformacion.on(eventToHearForTap, function(){
		$.mobile.changePage("#PaginaInfo");		
   })
   
   var BotonBackFromInformacion =  $("#BotonBackFromInformacion");   
   BotonBackFromInformacion.bind(eventToHearForTap, function(event){
		window.history.back(-1);
		event.preventDefault();
   })
   
   var BotonBackFromPaginaJuego =  $("#BotonBackFromPaginaJuego");   
   BotonBackFromPaginaJuego.bind(eventToHearForTap, function(event){
		window.history.back(-1);
		event.preventDefault();
   })
   
   
   var visitWebButton =  $("#visitWebButton");
   var visitWebButtonButton =  $("#visitWebButtonButton");
   var visitWebLink =  $("#visitWebLink");
   visitWebButtonButton.bind(eventToHearForTap, function(){
       //navigator.app.loadUrl('http://www.xploxion.com', { openExternal:true } ); 
       //window.external.Notify("OpenXploxionPage");
       if(inBrowser){
           window.open('http://www.xploxion.com/');
       }else{
           //Cordova.exec(null, null, "OpenXploxionPage", "", "");
           if (inW8StoreApp) {
               var uriToLaunch = "http://www.xploxion.com";
               var uri = new Windows.Foundation.Uri(uriToLaunch);
               Windows.System.Launcher.launchUriAsync(uri)
           }else if(inAndroidApp){
				window.open("http://www.xploxion.com", '_blank', 'location=yes');
		   }else{
               window.external.Notify("OpenXploxionPage");
           }
			
		}
		
   })
   
   
   var sonidoFondo = $("#sonidoFondo");
   var PaginaInicial =  $("#PaginaInicial");
   PaginaInicial.bind("pageshow", function(){
       //sonidoFondo.get(0).play();
   })   
   PaginaInicial.bind("pagebeforehide", function(){
       //sonidoFondo.get(0).pause();
   })
   //sonidoFondo.get(0).play();
   var PaginaJuego =  $("#PaginaJuego");
   PaginaJuego.bind("pageshow", function(){
       insidePaginaJuego = true;
   }) 
   PaginaJuego.bind("pagebeforeshow", function(){
	   //console.log("Juego "+CurrentGameToLoad+" seleccionado")	
	   CurrentGameInfo = listaDeJuegos.gamesList[0].desc;
       CurrentGameInfo.StartShow()
	   animCtxt.state = XplxnControlState.IDLE;
	   animCtxt.stateStartTime = new Date;
   }) 
   PaginaJuego.bind("pagebeforehide", function(){
       insidePaginaJuego = false;
   }) 
   //Preparar los evento de mouse del canvas
   canvas = $("#GameDrawCanvas");
   canvas.bind(eventToHearForMousePress, currentGameMousePressEvent);
   canvas.bind(eventToHearForMouseRelease, currentGameMouseReleaseEvent);
   canvas.bind(eventToHearForMouseMove, currentGameMouseMoveEvent);   
   //Empeiza el bucle del juego
   gameLoop();
})//fin de  on-doc-ready




